package com.sam.security;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.apache.struts2.config.ParentPackage;

import com.sam.core.BaseAction;
import com.sam.security.domain.AsyncTreeNode;
import com.sam.security.domain.Menu;
import com.sam.security.domain.TreeNode;
import com.sam.security.service.SecurityService;

/**
 * @project EMan
 * @author Sam
 * @version
 * @date Feb 7, 2009 4:13:37 PM
 */
@ParentPackage("default")
public class MenuAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	private SecurityService securityService;

	private Menu menu;

	/**
	 * 根据menuId删除菜单
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		securityService.deleteMenu(dataString);
		sendJSON(true);
		return JSON;
	}

	/**
	 * 查询menuId下级子菜单
	 * @return
	 */
	public String expand() {
		List<AsyncTreeNode> menus = securityService.querySubMenus(dataString);
		sendJSON(menus);
		return JSON;
	}

	/**
	 * 查询menuId下级子菜单,过滤checked(多选框)和children(下级菜单列表)字段
	 * @return
	 */
	public String expandWithoutCheckbox() {
		List<AsyncTreeNode> menus = securityService.querySubMenus(dataString);
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				if (name.equals("checked") || name.equals("children")) {
					return true;
				} else {
					return false;
				}
			}

		});
		sendJSON(menus, config);
		return JSON;
	}

	/**
	 * @return
	 */
	public String expandWithPermission() {
		List<AsyncTreeNode> menus = securityService.querySubMenusForPermission(dataString);
		if (menus == null || menus.size() == 0) {
			menus = securityService.querySubPermissions(dataString);
		}
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				if (name.equals("checked") || name.equals("children")) {
					return true;
				} else {
					return false;
				}
			}
		});
		sendJSON(menus, config);
		return JSON;
	}

	public Menu getMenu() {
		return menu;
	}

	public String load() {
		Menu menu = securityService.queryMenuInfo(dataString);
		sendJSON(1, menu);
		return JSON;
	}

	public String save() {
		JSONArray jsonObject = null;
		if (dataString != null && !dataString.equals("")) {
			TreeNode menu = new TreeNode();
			menu.setText("菜单管理");
			menu.setCls("folder");
			menu.setLeaf(true);
			menu.setLink("/EMan/js/modules/TreeNode.js");
			menu.setId("MENU_001_012_013");
			jsonObject = JSONArray.fromObject(menu);
		} else {
			TreeNode menu = new TreeNode();
			jsonObject = JSONArray.fromObject(menu);
		}

		try {
			jsonString = "{success:true,totalCount :1,list:" + jsonObject.toString() + "}";
		} catch (Exception e) {
			jsonString = "[]";
		}
		return JSON;
	}

	public String search() {
		List<AsyncTreeNode> menus = securityService.querySubMenus(dataString);
		JSONArray jsonObject = JSONArray.fromObject(menus);
		try {
			jsonString = jsonObject.toString();
		} catch (Exception e) {
			jsonString = "[]";
		}
		return JSON;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}

	public String update() {
		securityService.updateMenu(menu);
		sendJSON(true);
		return JSON;
	}

}
